package com.wang.head.controller;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wang.common.pojo.BasePojoEnum;
import com.wang.common.pojo.BaseQuery;
import com.wang.common.util.ConstatFinalUtil;
import com.wang.common.util.DateUtil;
import com.wang.common.util.RedisUtil;
import com.wang.common.web.controller.BaseController;
import com.wang.users.pojo.AtUsersDesc;
import com.wang.users.pojo.AtUsersDescQuery;
import com.wang.users.pojo.AtUsersLeave;
import com.wang.users.pojo.AtUsersLeaveQuery;
import com.wang.users.pojo.AtUsersWork;
import com.wang.users.pojo.AtUsersWorkEnum;
import com.wang.users.pojo.AtUsersWorkQuery;
import com.wang.users.service.AtUsersDescService;
import com.wang.users.service.AtUsersLeaveService;
import com.wang.users.service.AtUsersWorkService;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;


/**
 * 用户首页操作
 *
 * @author wangsh
 */
@Log4j2
@Controller
@RequestMapping("/head/users/")
public class UsersHeadController extends BaseController {
    @Autowired
    private AtUsersDescService usersDescService;
    @Autowired
    private AtUsersLeaveService usersLeaveService;
    @Autowired
    private AtUsersWorkService usersWorkService;

    /**
     * 首页
     *
     * @return
     */
    @RequestMapping("/main")
    public String main(HttpServletRequest request, Model model) {
        log.info("--main--");
        AtUsersDesc usersSess = (AtUsersDesc) this.findObjFromSession(request, BasePojoEnum.SESS_USERS);
        if (StringUtils.isEmpty(usersSess.getId())) {
            return "redirect:/login";
        }
        AtUsersDescQuery paramQuery = new AtUsersDescQuery();
        AtUsersDesc entity = paramQuery.getEntity();
        entity.setId(usersSess.getId());
        AtUsersDesc one = this.usersDescService.findOne(paramQuery);
        model.addAttribute(ConstatFinalUtil.FINAL_CODE_ONE, one);
        return "head/users/main";
    }

    /**
     * 欢迎页面
     *
     * @return
     */
    @RequestMapping("/welcome")
    public String welcome() {
        log.info("--welcome--");
        return "head/users/welcome";
    }

    /**
     * 打开用户更新
     *
     * @return
     */
    @RequestMapping("/usersUpdate")
    public String usersUpdate(HttpServletRequest request, AtUsersDescQuery paramQuery,Model model) {
        log.info("--usersUpdate--");

        /* 从session中获取信息 */
        AtUsersDesc one = (AtUsersDesc) this.findObjFromSession(request, BasePojoEnum.SESS_USERS);
        AtUsersDesc entity = paramQuery.getEntity();
        entity.setId(one.getId());
        one = this.usersDescService.findOne(paramQuery);
        model.addAttribute(ConstatFinalUtil.FINAL_CODE_ONE, one);
        model.addAttribute(ConstatFinalUtil.FINAL_CODE_PARAMQUERY, paramQuery);
        if ("updateInfo".equalsIgnoreCase(paramQuery.getOperType())) {
            /* 根据不同的请求跳转不同的页面 */
            return "head/users/usersUpdateInfo";
        } else if ("updatePass".equalsIgnoreCase(paramQuery.getOperType())) {
            /* 根据不同的请求跳转不同的页面 */
            return "head/users/usersUpdatePass";
        }
        return "head/users/usersInfo";
    }

    /**
     * 打开用户提交
     *
     * @return
     */
    @RequestMapping("/usersUpdateSubmit")
    public String usersUpdateSubmit(HttpServletRequest request,AtUsersDescQuery paramQuery, Model model) {
        log.info("--usersUpdateSubmit--");
        AtUsersDesc users = (AtUsersDesc) this.findObjFromSession(request, BasePojoEnum.SESS_USERS);
        /* 从session中获取信息 */
        AtUsersDesc one = (AtUsersDesc) this.findObjFromSession(request, BasePojoEnum.SESS_USERS);
        AtUsersDesc entityCond = paramQuery.getEntity();
        entityCond.setId(one.getId());
        one = this.usersDescService.findOne(paramQuery);
        model.addAttribute(ConstatFinalUtil.FINAL_CODE_ONE, one);
        model.addAttribute(ConstatFinalUtil.FINAL_CODE_PARAMQUERY, paramQuery);
        if (BaseQuery.FINAL_OPER_UPDATE.equalsIgnoreCase(paramQuery.getOperType())) {
            AtUsersDesc entity = paramQuery.getEntity();

            boolean dbFlag = this.usersDescService.updateById(entity);
            JSONObject resultJson = this.proccedResponseJson(dbFlag);
            log.info("==dbFlag=={}", dbFlag);
            model.addAttribute(ConstatFinalUtil.FINAL_RESPONSE, resultJson);
            return "head/users/usersUpdateInfo";
        }
        return "head/users/usersInfo";
    }

    /**
     * 退出页面
     *
     * @return
     */
    @RequestMapping("/logout")
    public String logout(HttpServletRequest request, HttpSession session) {
        log.info("--logout--");
        /* 获取用户 */
        /* 删除 */
        session.removeAttribute(BasePojoEnum.SESS_USERS.getCode());
        session.removeAttribute("lastLoginTime");
        /* 客户端跳转到登陆页面 */
        return "redirect:/login";
    }

    /**
     * 工作内容查询多条
     *
     * @return
     */
    @RequestMapping("/workList")
    public String workList(AtUsersWorkQuery paramQuery, Model model, HttpServletRequest request) {
        log.info("==工作内容列表==");
        AtUsersDesc usersDescSess = (AtUsersDesc) this.findObjFromSession(request, BasePojoEnum.SESS_USERS);
        /* 分页对象 */
        IPage<AtUsersWork> paramPage = null;
        try {
            paramPage = new Page<>(paramQuery.getCurrent(), paramQuery.getSize());
        } catch (Exception e) {
            paramPage = new Page<>(1, 20);
        }
        /* 时间处理 */
        if (StringUtils.isNotEmpty(paramQuery.getStDate())) {
            paramQuery.setPubTimeSt(this.dateUtil.strToDateTime(paramQuery.getStDate()));
        }
        paramQuery.addLike();

        /* 增加条件 */
        AtUsersWork entity = paramQuery.getEntity();
        entity.setUsersId(usersDescSess.getId());

        /* 查询对象 */
        List<AtUsersWork> dataList = this.usersWorkService.findList(paramPage, paramQuery);
        paramQuery.removeLike();
        /* 列表数据 */
        model.addAttribute(ConstatFinalUtil.FINAL_CODE_DATALIST, dataList);
        /* 分页数据 */
        model.addAttribute(ConstatFinalUtil.FINAL_CODE_PAGEINFOUTIL, paramPage);
        model.addAttribute(ConstatFinalUtil.FINAL_CODE_PARAMQUERY, paramQuery);
        model.addAttribute(ConstatFinalUtil.FINAL_CODE_ENUMSONE, new AtUsersWork());
        /* 查询数据 */
        return "head/users/workList";
    }

    /**
     * 工作内容打开添加页面
     * 更新和详情是同一个页面;
     *
     * @return
     */
    @RequestMapping("/workInsert")
    public String workInsert(AtUsersWorkQuery paramQuery, Model model, HttpServletRequest request) {
        log.info("==工作内容添加==");
        AtUsersWork entity = paramQuery.getEntity();
        String templateId = "";
        if (AtUsersWorkEnum.WORKTYPE_YUEBAO.getCode().equalsIgnoreCase(entity.getWorkType())) {
            templateId = ConstatFinalUtil.FINAL_CONFIG_YUEBAO;
        } else if (AtUsersWorkEnum.WORKTYPE_ZHOUBAO.getCode().equalsIgnoreCase(entity.getWorkType())) {
            templateId = ConstatFinalUtil.FINAL_CONFIG_ZHOUBAO;
        } else if (AtUsersWorkEnum.WORKTYPE_RIBAO.getCode().equalsIgnoreCase(entity.getWorkType())) {
            templateId = ConstatFinalUtil.FINAL_CONFIG_RIBAO;
        }
        /* 设置模板信息 */
        if (StringUtils.isNotEmpty(templateId)) {
            String info = (String) this.redisUtil.hashGet(RedisUtil.REDIS_KEY, templateId);
            JSONObject infoJson = JSON.parseObject(info);
            String templateStr = infoJson.getString(ConstatFinalUtil.FINAL_CONFIG_CONTENT);
            Map<String,String> paramsMap = new HashMap<>();
            /* info中有一个占位符;${message},模板字符串 */
            String resultInfo = this.regexUtil.replaceOperator(templateStr, paramsMap);
            entity.setContent(resultInfo);
        }
        model.addAttribute(ConstatFinalUtil.FINAL_CODE_ONE, entity);
        /* 查询数据 */
        return "head/users/workInsert";
    }

    /**
     * 工作内容打开更新页面
     * 更新和详情是同一个页面;
     *
     * @return
     */
    @RequestMapping("/workInsertSubmit")
    public String workInsertSubmit(AtUsersWorkQuery paramQuery, Model model, HttpServletRequest request) {
        log.info("==工作内容添加提交==");
        AtUsersWork entity = paramQuery.getEntity();
        /* 从session中获取信息 */
        AtUsersDesc one = (AtUsersDesc) this.findObjFromSession(request, BasePojoEnum.SESS_USERS);
        if (Objects.nonNull(one)) {
            entity.setUsersId(one.getId());
        }
        if (StringUtils.isNotEmpty(paramQuery.getCurrDate())) {
            if (AtUsersWorkEnum.WORKTYPE_YUEBAO.getCode().equalsIgnoreCase(entity.getWorkType())) {
                entity.setCurrDate(this.dateUtil.strToDate(paramQuery.getCurrDate(), DateUtil.DATE_MONTH_FORMAT));
            } else {
                entity.setCurrDate(this.dateUtil.strToDate(paramQuery.getCurrDate()));
            }
        }
        boolean dbFlag = this.usersWorkService.save(entity);
        JSONObject resultJson = this.proccedResponseJson(dbFlag);
        model.addAttribute(ConstatFinalUtil.FINAL_RESPONSE, resultJson);
        /* 查询数据 */
        return this.workInsert(paramQuery, model, request);
    }

    /**
     * 工作内容打开更新页面
     * 更新和详情是同一个页面;
     *
     * @return
     */
    @RequestMapping("/workUpdate")
    public String workUpdate(AtUsersWorkQuery paramQuery, Model model) {
        log.info("==工作内容更新==");
        /* 根据id查询 */
        AtUsersWork one = this.usersWorkService.findOne(paramQuery);
        model.addAttribute(ConstatFinalUtil.FINAL_CODE_ONE, one);
        model.addAttribute(ConstatFinalUtil.FINAL_CODE_PARAMQUERY, paramQuery);
        if (BaseQuery.FINAL_OPER_UPDATE.equalsIgnoreCase(paramQuery.getOperType())) {
            /* 查询数据 */
            return "head/users/workUpdate";
        }
        /* 查询数据 */
        return "head/users/workInfo";
    }

    /**
     * 工作内容打开更新页面
     * 更新和详情是同一个页面;
     *
     * @return
     */
    @RequestMapping("/workUpdateSubmit")
    public String workUpdateSubmit(AtUsersWorkQuery paramQuery, Model model) {
        log.info("==工作内容更新提交==");
        AtUsersWork entity = paramQuery.getEntity();
        if (StringUtils.isNotEmpty(paramQuery.getPubTime())) {
            entity.setPubTime(this.dateUtil.strToDateTime(paramQuery.getPubTime()));
        }
        if (StringUtils.isNotEmpty(paramQuery.getCurrDate())) {
            if (AtUsersWorkEnum.WORKTYPE_YUEBAO.getCode().equalsIgnoreCase(entity.getWorkType())) {
                entity.setCurrDate(this.dateUtil.strToDate(paramQuery.getCurrDate(), DateUtil.DATE_MONTH_FORMAT));
            } else {
                entity.setCurrDate(this.dateUtil.strToDate(paramQuery.getCurrDate()));
            }
        }
        /* 根据id查询 */
        boolean dbFlag = this.usersWorkService.updateById(entity);
        JSONObject resultJson = this.proccedResponseJson(dbFlag);
        model.addAttribute(ConstatFinalUtil.FINAL_RESPONSE, resultJson);
        /* 查询数据 */
        return this.workUpdate(paramQuery, model);
    }

    /**
     * 请假查询多条
     *
     * @return
     */
    @RequestMapping("/leaveList")
    public String leaveList(AtUsersLeaveQuery paramQuery, Model model, HttpServletRequest request) {
        log.info("==请假列表==");
        AtUsersDesc usersDescSess = (AtUsersDesc) this.findObjFromSession(request, BasePojoEnum.SESS_USERS);
        /* 分页对象 */
        IPage<AtUsersLeave> paramPage = null;
        try {
            paramPage = new Page<>(paramQuery.getCurrent(), paramQuery.getSize());
        } catch (Exception e) {
            paramPage = new Page<>(1, 20);
        }
        /* 时间处理 */
        if (StringUtils.isNotEmpty(paramQuery.getStDate())) {
            paramQuery.setPubTimeSt(this.dateUtil.strToDateTime(paramQuery.getStDate()));
        }
        paramQuery.addLike();

        /* 增加条件 */
        AtUsersLeave entity = paramQuery.getEntity();
        entity.setUsersId(usersDescSess.getId());

        /* 查询对象 */
        List<AtUsersLeave> dataList = this.usersLeaveService.findList(paramPage, paramQuery);
        paramQuery.removeLike();
        /* 列表数据 */
        model.addAttribute(ConstatFinalUtil.FINAL_CODE_DATALIST, dataList);
        /* 分页数据 */
        model.addAttribute(ConstatFinalUtil.FINAL_CODE_PAGEINFOUTIL, paramPage);
        model.addAttribute(ConstatFinalUtil.FINAL_CODE_PARAMQUERY, paramQuery);
        model.addAttribute(ConstatFinalUtil.FINAL_CODE_ENUMSONE, new AtUsersLeave());
        /* 查询数据 */
        return "head/users/leaveList";
    }

    /**
     * 请假打开添加页面
     * 更新和详情是同一个页面;
     *
     * @return
     */
    @RequestMapping("/leaveInsert")
    public String leaveInsert(AtUsersLeaveQuery paramQuery, Model model, HttpServletRequest request) {
        log.info("==请假添加==");
        AtUsersLeave entity = paramQuery.getEntity();
        model.addAttribute(ConstatFinalUtil.FINAL_CODE_ONE, entity);
        /* 查询数据 */
        return "head/users/leaveInsert";
    }

    /**
     * 请假打开更新页面
     * 更新和详情是同一个页面;
     *
     * @return
     */
    @RequestMapping("/leaveInsertSubmit")
    public String leaveInsertSubmit(AtUsersLeaveQuery paramQuery, Model model, HttpServletRequest request) {
        log.info("==请假添加提交==");
        AtUsersLeave entity = paramQuery.getEntity();
        /* 从session中获取信息 */
        AtUsersDesc one = (AtUsersDesc) this.findObjFromSession(request, BasePojoEnum.SESS_USERS);
        if (Objects.nonNull(one)) {
            entity.setUsersId(one.getId());
        }
        if (StringUtils.isNotEmpty(paramQuery.getLeaveStartDate())) {
            entity.setLeaveStartDate(this.dateUtil.strToDate(paramQuery.getLeaveStartDate()));
        }
        if (StringUtils.isNotEmpty(paramQuery.getLeaveEndDate())) {
            entity.setLeaveEndDate(this.dateUtil.strToDate(paramQuery.getLeaveEndDate()));
        }
        boolean dbFlag = this.usersLeaveService.save(entity);
        JSONObject resultJson = this.proccedResponseJson(dbFlag);
        model.addAttribute(ConstatFinalUtil.FINAL_RESPONSE, resultJson);
        /* 查询数据 */
        return this.leaveInsert(paramQuery, model, request);
    }

    /**
     * 请假打开更新页面
     * 更新和详情是同一个页面;
     *
     * @return
     */
    @RequestMapping("/leaveUpdate")
    public String leaveUpdate(AtUsersLeaveQuery paramQuery, Model model) {
        log.info("==请假更新==");
        /* 根据id查询 */
        AtUsersLeave one = this.usersLeaveService.findOne(paramQuery);
        model.addAttribute(ConstatFinalUtil.FINAL_CODE_ONE, one);
        model.addAttribute(ConstatFinalUtil.FINAL_CODE_PARAMQUERY, paramQuery);
        if (BaseQuery.FINAL_OPER_UPDATE.equalsIgnoreCase(paramQuery.getOperType())) {
            /* 查询数据 */
            return "head/users/leaveUpdate";
        }
        /* 查询数据 */
        return "head/users/leaveInfo";
    }

    /**
     * 请假打开更新页面
     * 更新和详情是同一个页面;
     *
     * @return
     */
    @RequestMapping("/leaveUpdateSubmit")
    public String leaveUpdateSubmit(AtUsersLeaveQuery paramQuery, Model model) {
        log.info("==请假更新提交==");
        AtUsersLeave entity = paramQuery.getEntity();
        if (StringUtils.isNotEmpty(paramQuery.getPubTime())) {
            entity.setPubTime(this.dateUtil.strToDateTime(paramQuery.getPubTime()));
        }
        if (StringUtils.isNotEmpty(paramQuery.getLeaveStartDate())) {
            entity.setLeaveStartDate(this.dateUtil.strToDate(paramQuery.getLeaveStartDate()));
        }
        if (StringUtils.isNotEmpty(paramQuery.getLeaveEndDate())) {
            entity.setLeaveEndDate(this.dateUtil.strToDate(paramQuery.getLeaveEndDate()));
        }
        /* 根据id查询 */
        boolean dbFlag = this.usersLeaveService.updateById(entity);
        JSONObject resultJson = this.proccedResponseJson(dbFlag);
        model.addAttribute(ConstatFinalUtil.FINAL_RESPONSE, resultJson);
        /* 查询数据 */
        return this.leaveUpdate(paramQuery, model);
    }
}
